/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ads.graph;

import ads.graph.Graph.Edge;
import ads.graph.Graph.Node;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author framet-lap
 */
public class GraphToolBox {

    public static Graph createGraphWithFile(String fileName) throws FileNotFoundException, IOException {

        //code from http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
        FileInputStream fstream = new FileInputStream(fileName);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        Graph graph = new Graph();
        //Read File Line By Line
        ArrayList<String> lines = new ArrayList<>();
        while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                strLine = new String(strLine.getBytes(), "UTF-8");
                lines.add(strLine);
//                System.out.println(strLine);
                String[] input = strLine.split(" ");
            try {
                graph.addNode(input[0]);
            } catch (NodeAlreadyDefinedException ex) {
                //Logger.getLogger(GraphToolBox.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                graph.addNode(input[1]);
            } catch (NodeAlreadyDefinedException ex) {
                //Logger.getLogger(GraphToolBox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        //zweite runde schölölö
        for (String s : lines) {
           
            // Print the content on the console
//                System.out.println(strLine);
            String[] input = s.split(" ");

            Node node = graph.getNode(input[0]);
            Node target = graph.getNode(input[1]);
            if (input.length >= 3) {
                try {
                    graph.addEdge(node.getLabel(), target.getLabel(), Integer.valueOf(input[2]));
                } catch (EdgeAlreadyDefinedException ex) {
                   // Logger.getLogger(GraphToolBox.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    graph.addEdge(target.getLabel(), node.getLabel(), Integer.valueOf(input[2]));
                } catch (EdgeAlreadyDefinedException ex) {
                    //Logger.getLogger(GraphToolBox.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                try {
                    graph.addEdge(node.getLabel(), target.getLabel());
                } catch (EdgeAlreadyDefinedException ex) {
                    //Logger.getLogger(GraphToolBox.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    graph.addEdge(target.getLabel(), node.getLabel());
                } catch (EdgeAlreadyDefinedException ex) {
                   // Logger.getLogger(GraphToolBox.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
           

        }
        return graph;
    }
}
